package com.geniusk.system.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.geniusk.security.annotation.PreDataAuth;
import com.geniusk.system.model.mapper.DeviceMapper;
import com.geniusk.system.model.pojo.Device;
import com.geniusk.system.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {

    @Value("${ezviz.app-key}")
    private String appKey;

    @Value("${ezviz.app-secret}")
    private String appSecret;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public String selectLatestFirmwareUrl(Long deviceId) {
        String contentUrl = "http://www.geniusk.com/iot-platform-api/system/device/firmware/";
        return contentUrl + deviceMapper.selectLatestFirmwareUrl(deviceId);
    }

    @Override
    public String selectLatestFirmwareVersion(Long deviceId) {
        return deviceMapper.selectLatestFirmwareVersion(deviceId);
    }

    @Override
    public Device selectDeviceById(Long deviceId) {
        return deviceMapper.selectDeviceById(deviceId);
    }

    @Override
    @PreDataAuth(groupAlias = "g", userAlias = "u")
    public List<Device> selectDeviceByPage(Device device) {
        return deviceMapper.selectDeviceByPage(device);
    }

    @Override
    public List<Device> selectPublicDeviceByPage() {
        return deviceMapper.selectPublicDeviceByPage();
    }

    @Override
    public int insertDevice(Device device) {
        return deviceMapper.insertDevice(device);
    }

    @Override
    public int updateDevice(Device device) {
        return deviceMapper.updateDevice(device);
    }

    @Override
    public int deleteDeviceByIds(Long[] ids) {
        return deviceMapper.deleteDeviceByIds(ids);
    }

    @Override
    public String getAccessToken(Long deviceId){

        String accessToken = deviceMapper.selectAccessTokenById(deviceId);

        if(accessToken == null) {
            String url="https://open.ys7.com/api/lapp/token/get";
            String result = HttpRequest.post(url)
                    .contentType("application/x-www-form-urlencoded")
                    .form("appKey", appKey)
                    .form("appSecret", appSecret)
                    .execute().body();
            JSONObject json= JSONUtil.parseObj(result);
            String data = json.getStr("data");
            JSONObject dataJSON = JSONUtil.parseObj(data);
            accessToken = dataJSON.getStr("accessToken");
            if(accessToken==null|| accessToken.isEmpty()){
                throw new RuntimeException("视频授权凭证错误");
            }
            Date expireTime = new Date(Long.parseLong(dataJSON.getStr("expireTime")));
            HashMap<String,Object> param = new HashMap<>();
            param.put("deviceId",deviceId);
            param.put("accessToken",accessToken);
            param.put("expireTime",expireTime);
            deviceMapper.updateAccessToken(param);
        }
        return accessToken;
    }
}
